package com.example.happ.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.happ.mapper.UserInfoMapper;
import com.example.happ.pojo.User;
import com.example.happ.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * @Package：com.example.demo.service.impl
 * @Project：demo
 * @name：王志迎
 * @Date：2024/12/24 9:37
 * @Filename：saveUserServiceImpl
 */


@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserInfoMapper userInfoMapper;

    @Override
    public boolean updateUserInfo(Map<String, String> map) {
        System.out.println("修改用户消息收到的数据："+map);
        User user = new User();
        user.setNickname(map.get("nickname"));
        user.setAvatar(map.get("avatar"));
        user.setPhone(map.get("phone"));
        user.setUpdatedAt(new Date());
        UpdateWrapper<User> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",map.get("id"));
        System.out.println("修改用户消息收到的user："+user);
        System.out.println(userInfoMapper.update(user,updateWrapper));
        return  userInfoMapper.update(user,updateWrapper)>0?true:false;
    }

    @Override
    public int findUserInformationByOpenid(String openId) {
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("wechat_id",openId);
        User user=userInfoMapper.selectOne(queryWrapper);
        System.out.println("寻找用户是否绑定过返回的数据："+user);
        return user==null?0:user.getId();
    }

    @Override
    public int insertUserInformationByOpenid(String openId, String avatarUrl, String nickName, String phone) {
        System.out.println("保存用户消息收到的数据："+openId+"  "+avatarUrl+"  "+nickName+"  "+phone);
        User user = new User();
        user.setWechatId(openId);
        user.setNickname(nickName);
        user.setAvatar(avatarUrl);
        user.setCreatedAt(new Date());
        user.setPhone(phone);
        userInfoMapper.insert(user);
        return findUserInformationByOpenid(openId);
    }

    @Override
    public User getUserInfo(Integer id) {
        QueryWrapper queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return userInfoMapper.selectOne(queryWrapper);
    }

}
